package com.dbq.fast.fcm;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.RingtoneManager;
import android.net.Uri;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;

import com.dbq.fast.R;
import com.dbq.fast.business.coupons.CouponsActivity;
import com.dbq.fast.business.main.MainActivity;
import com.dbq.fast.common.bean.User;
import com.yiba.vpn.utils.Constants;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.yiba.vpn.core.LocalVpnService;
import com.yiba.vpn.protocal.DBQTestTimeDelay;
import com.yiba.vpn.utils.RxBus;

import java.util.Map;


/**
 * 推送业务
 * Created by Widsom Zhang on 2017/10/31.
 */

public class MyFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (remoteMessage != null) {
            Map<String, String> data = remoteMessage.getData();// data可以不要
            if (data != null && data.size() > 0) {
                String key = data.get("key");// key: EXPIRE 表示过期
                String account = data.get("account");
                String title = data.get("title");
                String body = data.get("body");
                String userAccount = User.getInstance().getAccount();
                if (TextUtils.equals(key, "EXPIRE") && TextUtils.equals(account, userAccount)) {
//                    sendNotification(getString(R.string.app_name), getString(R.string.firebase_message_expire),key);
                    sendNotification(title, body, key);
//                    if (remoteMessage.getNotification() != null) {
//                        String title = remoteMessage.getNotification().getTitle();
//                        String body = remoteMessage.getNotification().getBody();
//                        sendNotification(title, body);
//                    }
                    MainActivity.isNotificationOutDate = true;
                    LocalVpnService.IsRunning = false;
                    DBQTestTimeDelay.IsRunning = false;
                    User.getInstance().setUserSetMealAvailableTime(0);
                    RxBus.get().post(Constants.RXBUS_START_VPN, Constants.RXBUS_SERVICE_OUT_DATE);
                } else if (TextUtils.equals(key, "SHARE") && TextUtils.equals(account, userAccount)) { //邀请码推送
                    sendNotification(getString(R.string.app_name), ""/*getString(R.string.firebase_message_share)*/, key);
                } else {
                    sendNotification(getString(R.string.app_name), "body", key);
                }
            }
        }
    }

    private void sendNotification(String messageTitle, String messageBody, String key) {
        if (TextUtils.isEmpty(messageTitle) || TextUtils.isEmpty(messageBody)) return;
        Intent intent;
        switch (key) {
            case "EXPIRE":
                intent = new Intent(getApplicationContext(), MainActivity.class);
                break;
            case "SHARE":
                intent = new Intent(getApplicationContext(), CouponsActivity.class);
                intent.putExtra("page", "1");
                break;
            default:
                intent = new Intent(getApplicationContext(), MainActivity.class);
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.mipmap.app_icon)
                .setContentTitle(messageTitle)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.notify(0, notificationBuilder.build());
        }
    }
}
